import { defineStore } from 'pinia'
import { mistakeApi } from '@/api/mistake'

export const useMistakeStore = defineStore('mistake', {
  state: () => ({
    subjects: [], // 科目列表
    questions: [], // 错题列表
    total: 0, // 总数
    currentPage: 1, // 当前页
    pageSize: 10, // 每页数量
    currentSubject: null, // 当前选中的科目
    searchKeyword: '', // 搜索关键词
    loading: false, // 加载状态
    uploadProgress: 0 // 上传进度
  }),

  getters: {
    // 获取科目名称
    getSubjectName: (state) => (subjectId) => {
      const subject = state.subjects.find(s => s.id === subjectId)
      return subject ? subject.name : '未知科目'
    },

    // 过滤后的错题列表
    filteredQuestions: (state) => {
      return state.questions
    }
  },

  actions: {
    // 获取所有科目
    async fetchSubjects() {
      try {
        const response = await mistakeApi.getAllSubjects()
        this.subjects = response.data || []
        return response
      } catch (error) {
        console.error('获取科目列表失败:', error)
        throw error
      }
    },

    // 创建科目
    async createSubject(subjectData) {
      try {
        const response = await mistakeApi.createSubject(subjectData)
        // 刷新科目列表
        await this.fetchSubjects()
        return response
      } catch (error) {
        console.error('创建科目失败:', error)
        throw error
      }
    },

    // 获取错题列表
    async fetchQuestions(params = {}) {
      try {
        this.loading = true
        const requestParams = {
          subjectId: this.currentSubject?.id,
          keyword: this.searchKeyword,
          page: this.currentPage,
          size: this.pageSize,
          ...params
        }

        const response = await mistakeApi.getQuestions(requestParams)
        this.questions = response.data?.records || []
        this.total = response.data?.total || 0
        return response
      } catch (error) {
        console.error('获取错题列表失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 创建错题
    async createQuestion(questionData) {
      try {
        const response = await mistakeApi.createQuestion(questionData)
        // 刷新错题列表
        await this.fetchQuestions()
        return response
      } catch (error) {
        console.error('创建错题失败:', error)
        throw error
      }
    },

    // 删除错题
    async deleteQuestion(questionId) {
      try {
        const response = await mistakeApi.deleteQuestion(questionId)
        // 刷新错题列表
        await this.fetchQuestions()
        return response
      } catch (error) {
        console.error('删除错题失败:', error)
        throw error
      }
    },

    // 上传图片
    async uploadImage(file, subjectId) {
      try {
        this.uploadProgress = 0
        const response = await mistakeApi.uploadImage(file, subjectId)
        return response
      } catch (error) {
        console.error('上传图片失败:', error)
        throw error
      } finally {
        this.uploadProgress = 0
      }
    },

    // 设置当前科目
    setCurrentSubject(subject) {
      this.currentSubject = subject
      this.currentPage = 1
    },

    // 设置搜索关键词
    setSearchKeyword(keyword) {
      this.searchKeyword = keyword
      this.currentPage = 1
    },

    // 重置搜索条件
    resetSearch() {
      this.currentSubject = null
      this.searchKeyword = ''
      this.currentPage = 1
    },

    // 设置分页
    setPage(page) {
      this.currentPage = page
    },

    // 设置每页数量
    setPageSize(size) {
      this.pageSize = size
      this.currentPage = 1
    }
  }
})